package com.jcfy.project.module.controller;

import com.jcfy.common.utils.SecurityUtils;
import com.jcfy.common.utils.poi.ExcelUtil;
import com.jcfy.framework.aspectj.lang.annotation.Log;
import com.jcfy.framework.aspectj.lang.enums.BusinessType;
import com.jcfy.framework.web.controller.BaseController;
import com.jcfy.framework.web.domain.AjaxResult;
import com.jcfy.framework.web.page.TableDataInfo;
import com.jcfy.project.module.domain.TDictData;
import com.jcfy.project.module.service.ITDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典数据Controller
 *
 * @author hushuai
 * @date 2020-07-06
 */
@RestController
@RequestMapping("/module/dict/data" )
public class TDictDataController extends BaseController {
    @Autowired
    private ITDictDataService tDictDataService;

    /**
     * 查询字典数据列表
     */
    @PreAuthorize("@ss.hasPermi('module:dict:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(TDictData tDictData) {
        startPage();
        List<TDictData> list = tDictDataService.selectTDictDataList(tDictData);
        return getDataTable(list);
    }
    
    /**
     * 导出字典数据列表
     */
    @PreAuthorize("@ss.hasPermi('module:dict:export')" )
    @Log(title = "字典数据-功能模块" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(TDictData tDictData) {
        List<TDictData> list = tDictDataService.selectTDictDataList(tDictData);
        ExcelUtil<TDictData> util = new ExcelUtil<TDictData>(TDictData. class);
        return util.exportExcel(list, "data" );
    }

    /**
     * 获取字典数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:dict:query')" )
    @GetMapping(value = "/{dictCode}" )
    public AjaxResult getInfo(@PathVariable("dictCode" ) Long dictCode) {
        return AjaxResult.success(tDictDataService.selectTDictDataById(dictCode));
    }

    /**
     * 新增字典数据
     */
    @PreAuthorize("@ss.hasPermi('module:dict:add')" )
    @Log(title = "字典数据-功能模块" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDictData tDictData) {
        tDictData.setCreateBy(SecurityUtils.getUsername());
        return toAjax(tDictDataService.insertTDictData(tDictData));
    }

    /**
     * 修改字典数据
     */
    @PreAuthorize("@ss.hasPermi('module:dict:edit')" )
    @Log(title = "字典数据-功能模块" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDictData tDictData) {
        tDictData.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(tDictDataService.updateTDictData(tDictData));
    }

    /**
     * 删除字典数据
     */
    @PreAuthorize("@ss.hasPermi('module:dict:remove')" )
    @Log(title = "字典数据-功能模块" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}" )
    public AjaxResult remove(@PathVariable Long[] dictCodes) {
        return toAjax(tDictDataService.deleteTDictDataByIds(dictCodes));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/dictType/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType) {
        return AjaxResult.success(tDictDataService.selectTDictDataByType(dictType));
    }
}
